﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kokomo.PeInspector.ObjectGraphs
{
	class ObjectTemplate<TObject> : NodeTemplate<TObject>
	{
		public ObjectTemplate(
			IEnumerable<MemberTemplate> memberTemplates = null
			)
		{
			this.MemberTemplates = new List<MemberTemplate>();
			if (memberTemplates != null) this.MemberTemplates.AddRange(memberTemplates);
		}

		public List<MemberTemplate> MemberTemplates { get; private set; }

		public override bool Inspect(object obj)
		{
			if (obj == null) return false;

			if (!(obj is TObject)) return false;
			TObject typed = (TObject)obj;

			bool matches = true;
			foreach (var memberTemplate in this.MemberTemplates)
			{
				if (memberTemplate != null)
				{
					matches &= memberTemplate.Inspect(obj);
				}
			}

			if (matches) this.CapturedValue = typed;

			return matches;
		}
	}
}
